﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Cheetah_Fast_Banking_App
{
    public partial class FormModifyAccount : Form
    {
        #region Constructor
        public FormModifyAccount()
        {
            InitializeComponent();
            RefreshItems();
        }
        #endregion // Constructor

        #region Private Methods
        private void EnableDisableControls(bool enabled)
        {
            textBoxPassword.Enabled = enabled;
            textBoxConfirmPassword.Enabled = enabled;
            buttonChangePassword.Enabled = enabled;
            buttonCloseAccount.Enabled = enabled;
        }

        private void RefreshItems()
        {
            int rowIndex;
            dataGridViewAccounts.Rows.Clear();
            foreach (Account account in DataSourceSingleton.GetInstance().Accounts)
            {
                rowIndex = dataGridViewAccounts.Rows.Add(account.AccountName);
                dataGridViewAccounts.Rows[rowIndex].Tag = account;
            }
        }
        #endregion // Private Methods

        #region Form Events
        private void dataGridViewAccounts_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex < 0)
            {
                // Row was deselected--clear text boxes and disable controls
                textBoxAccountName.Text = "";
                textBoxPassword.Text = "";
                textBoxConfirmPassword.Text = "";
                EnableDisableControls(false);
            }
            else
            {
                // Row was selected--use the associated account tag to populate the textbox
                Account account = dataGridViewAccounts.Rows[e.RowIndex].Tag as Account;
                if (account != null)
                {
                    textBoxAccountName.Text = account.AccountName;
                }
                EnableDisableControls(true);
            }
        }

        private void buttonCloseAccount_Click(object sender, EventArgs e)
        {
            string error = DataSourceSingleton.GetInstance().CloseAccount(textBoxAccountName.Text);
            if (string.IsNullOrEmpty(error))
            {
                // No error was encountered--indicate that account was successfully closed.
                MessageBox.Show("Account has been sucessfully closed.", "Account Closed",
                    MessageBoxButtons.OK, MessageBoxIcon.Information);
                textBoxAccountName.Text = "";
                EnableDisableControls(false);
                RefreshItems();
            }
            else
            {
                // Display error message
                MessageBox.Show(error, "Error Closing Account", MessageBoxButtons.OK, MessageBoxIcon.Error);
                textBoxPassword.Text = "";
                textBoxConfirmPassword.Text = "";
            }
        }

        private void buttonChangePassword_Click(object sender, EventArgs e)
        {
            string error = DataSourceSingleton.GetInstance().ChangeAccountPassword(textBoxAccountName.Text, textBoxPassword.Text, textBoxConfirmPassword.Text);
            if (string.IsNullOrEmpty(error))
            {
                // No error was encountered--indicate that the password has changed
                MessageBox.Show("Password has been successfully changed.", "Password Changed",
                    MessageBoxButtons.OK, MessageBoxIcon.Information);
                textBoxPassword.Text = "";
                textBoxConfirmPassword.Text = "";
            }
            else
            {
                // Display error message
                MessageBox.Show(error, "Error Changing Password", MessageBoxButtons.OK, MessageBoxIcon.Error);
                textBoxPassword.Text = "";
                textBoxConfirmPassword.Text = "";
                textBoxPassword.Focus();
            }
        }
        #endregion // Form Events
    }
}
